// 用一个结构体来存学生姓名，学号，分数三个信息。
// 用冒泡进行成绩从低到高排序。
// 用折半找分数为87的学生信息(结构数组解决:读入考生名字，编号，成绩。冒泡按成绩排序，然后输出成绩为x的所有考生信息)

// 16-1
#include "stdio.h"

typedef struct Student{
    int num;
    float grade;
}Student;

void sort(Student stus[],int n){
    for (int i=0; i<n; i++) {
        for (int j=i;j<n;++j){
            if (stus[i].grade > stus[j].grade) {
                Student stu = stus[i];
                stus[i] = stus[j];
                stus[j] = stu;
            }
        }
    }
}

double absolute(double a){
    return a > 0 ? a: -a;
}


int search(Student stus[],int n, float grade){
    int left = 0, right = n-1;
    int mid;
    while (left <= right) {
        // mid = (left + right) / 2;
        // 避免溢出
        mid = left + (right - left) / 2;
        if (absolute(stus[mid].grade - grade) < 0.000001) {
            return stus[mid].num;
        }
        else if (stus[mid].grade < grade) {
            left = mid + 1;
        }else {
            right = mid - 1;
        }
    }
    return -1;
}



int main(){
    int n = 6;
    Student stus[] = {
        1,60,
        2,45,
        3,80,
        4,87,
        5,90,
        6,56
    };
    sort(stus, n);
    int num = search(stus, n, 56);
    printf("%d\n", num);
    // for (int i=0; i<n; i++) {
    //     printf("%d %g\n",stus[i].num, stus[i].grade);
    // }
    

    return 0;
}